import {
  getCompetiorData,
  apiGetCompetiorData,
  apiGetAddCompetiorData,
  apiUpdateCompetior,
  apiDeleteCompetior,
} from '@/services/api';

export default {
  namespace: 'Competior',

  state: {
    competiorData: {},
    updateDidFinish: false,
  },

  effects: {
    *queryorderList({ payload }, { call, put }) {
      const response = yield call(apiGetCompetiorData, payload);
      // console.log('获取数据', response);
      if (response) {
        yield put({
          type: 'setQueryorderList',
          payload: {
            competiorData: response.data,
          },
        });
      }
    },
    *addCompetior({ payload }, { call, put }) {
      const response = yield call(apiGetAddCompetiorData, payload);
      yield put({
        type: 'setPayload',
        payload: {
          updateDidFinish: new Date().getTime(),
        },
      });
      // console.log('获取数据', response);
    },
    *updateCompetior({ payload }, { call, put }) {
      const response = yield call(apiUpdateCompetior, payload);
      yield put({
        type: 'setPayload',
        payload: {
          updateDidFinish: new Date().getTime(),
        },
      });
    },
    *deleteCompetior({ payload }, { call, put }) {
      const response = yield call(apiDeleteCompetior, payload);
      console.log('删除：', payload);
      yield put({
        type: 'setPayload',
        payload: {
          updateDidFinish: new Date().getTime(),
        },
      });
      // console.log('获取数据', response);
    },
  },

  reducers: {
    setPayload(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    setCompetior(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    setQueryorderList(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    logPayload(state, action) {
      console.log(action.payload);
      return state;
    },
  },
};
